package com.itheima;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.BufferedReader;
import java.io.FileReader;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
public class DeptController {
    // test.test-branch提交
    @RequestMapping("/depts")
    public String list2() throws Exception {
        //加载文件,获取原始数据
        FileReader fr = new FileReader("E:\\课程\\Java Web\\dept.txt");
        BufferedReader bfr = new BufferedReader(fr);
        String len;
        List<String> lines = new ArrayList<>();
        while ((len = bfr.readLine())!=null){
            lines.add(len);
        }
        //对原始数据进行处理,组装部门数据
        List<Dept> deptList = lines.stream().map(line -> {
            String[] parts = line.split(",");
            Integer id = Integer.parseInt(parts[0]);
            String name = parts[1];
            /*LocalDateTime updateTime = LocalDateTime.parse(parts[2],
                    DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));*/
            return new Dept(id, name, parts[2]);
        }).toList();
        //响应数据
        return deptList.toString();
    }
}
